package base_demo.waitandnotify;

public class Food extends Thread{
    @Override
    public void run() {
        //1.循环
        //2.锁代码块
        //3.拆解
        while(true){
         synchronized (Desk.lock){
             //判断是否有面条
             if(Desk.foodFlag == 1){
                 //判断是否吃了10碗
                 if(Desk.count == 0){
                     break;
                 } else {
                     Desk.count--;
                     System.out.println(getName() + "正在吃第" + (10 - Desk.count) + "碗,还能吃" + Desk.count + "碗!!!");
                     //将状态改变
                     Desk.foodFlag = 0;
                     Desk.lock.notifyAll();


                 }
             } else {
                 //没有面条
                 //进行等待并唤醒其他线程
                 try {
                     Desk.lock.wait();
                 } catch (InterruptedException e) {
                     throw new RuntimeException(e);
                 }
                 Desk.lock.notifyAll();

             }
         }
        }
    }
}
